package com.example.dell.controllsmall.activity;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.example.dell.controllsmall.activity.db.base.BaseManager;
import com.example.dell.controllsmall.activity.dbhelper.DBHelper;
import com.iflytek.cloud.Setting;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.ocean.mvp.library.net.NetClient;
import com.ocean.mvp.library.net.NetMessage;
import com.ocean.mvp.library.net.SendRequestListener;
import com.youdao.sdk.app.YouDaoApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2017/9/26.
 */

public class ControllApplication extends Application implements SendRequestListener {

    private NetClient client;
    private DBHelper dbHelper;

    // 保存所有的Activity
    private List<Activity> activityList;

    @Override
    public void onCreate() {
        super.onCreate();
        client = NetClient.getInstance(this);
        client.setSendRequestListener(this);

        /**
         * 讯飞初始化
         * 原始app_id = 595c594c
         */
        SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID + "=599d6109" + "," + SpeechConstant.ENGINE_MODE + "=" + SpeechConstant.MODE_MSC);
        Setting.setShowLog(false);


        // 以下语句用于设置日志开关（默认开启），设置成false时关闭语音云SDK日志打印
        // Setting.setShowLog(false);
        YouDaoApplication.init(this, getResources().getString(R.string.app_youdao_id));//创建应用，每个应用都会有一个Appid，绑定对应的翻译服务实例，即可使用

        BaseManager.initOpenHelper(this);
    }


    /**
     * 获取的app的Application对象
     *
     * @param context 上下文
     * @return Application对象
     */
    public static ControllApplication from(Context context) {
        return (ControllApplication) context.getApplicationContext();
    }

    /**
     * 添加activity到activityList集合中
     *
     * @param activity 每一個activity
     */

    public void addActivity(Activity activity) {
        if (activityList == null) {
            activityList = new ArrayList<Activity>();
        }
        activityList.add(activity);
    }

    /**
     * 清空列表，取消引用
     */
    public void clearActivity() {
        activityList.clear();
    }

    /**
     * app退出
     */
    public void exit() {
        for (Activity activity : activityList) {
            if (!activity.isFinishing() && activity != null) {
                activity.finish();
            }
        }
//        clearActivity();
        System.exit(0);
    }

    /**
     * 获取数据库操作类
     *
     * @return 数据库操作类
     */

    public synchronized DBHelper getDataBase() {
        if (dbHelper == null)
            dbHelper = new DBHelper(getApplicationContext());
        return dbHelper;
    }

    /**
     * 获取访问网络对象
     *
     * @return 访问网络对象
     */
    public NetClient getNetClient() {
        return client;
    }

    public void setNetClient() {
        client = NetClient.getInstance(getApplicationContext());
        client.setSendRequestListener(this);
    }

    @Override
    public void onSending(NetMessage message, long total, long current) {

    }

    @Override
    public void onSuccess(NetMessage message, String result) {

    }

    @Override
    public void onFail(NetMessage message, int errorCode, String errorMessage) {

    }
}